// ------------------------------------------------------------------------------
// 
// Copyright (c) 2006-2008 Swampware, Inc.
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// 
// ------------------------------------------------------------------------------
using System;

namespace Water
{
	/// <summary>
	/// Summary description for StatementParser.
	/// </summary>
	public class StatementParser
	{
		private Water.TextReader _reader;
		private Water.Environment _environment;

		public StatementParser(Water.TextReader reader, Water.Environment environment)
		{
			this._reader = reader;
			this._environment = environment;
		}

		public Water.Statement Parse()
		{
			#region Parse

			Water.Parser.TrimWhitespace(this._reader);

			if(!this._reader.CanRead)
			{
				return null;
			}

			char ch = (char)this._reader.Peek();

			if(ch == ';')
			{
				Water.Parser.ParseComment(this._reader);

				// Try it again.
				return Parse();
			}
			else if(ch == '(')
			{
				string file = this._reader.Filename;
				int line = this._reader.LineNumber;
				int column = this._reader.ColumnNumber;
				Water.List expressions = Water.Parser.ParseList(this._reader);

				return new Water.Statement(file, line, column, expressions);
			}
			else
			{
				return ParseStatement(this._reader, this._environment);
			}

			#endregion
		}

		private static Water.Statement ParseStatement(Water.TextReader reader, Water.Environment environment)
		{
			Water.Parser.TrimWhitespace(reader);

			if(!reader.CanRead)
			{
				return null;
			}

			char ch = (char)reader.Peek();

			if(ch == ';')
			{
				string comment = Water.Parser.ParseComment(reader);

				// Try it again.
				return ParseStatement(reader, environment);
			}

			string file = reader.Filename;
			int line = reader.LineNumber;
			int column = reader.ColumnNumber;
			Water.List expressions = new Water.List();

			while(reader.CanRead)
			{
				if(Water.Parser.IsNewline(reader))
				{
					Water.Parser.TrimNewline(reader);
					break;
				}

				if(Water.Parser.IsComment(reader))
				{
					Water.Parser.ParseComment(reader);
					break;
				}

				expressions.Add(Water.Parser.ParseExpression(reader));

				Water.Parser.TrimSpace(reader);
			}

			if(expressions.Count == 0)
			{
				// Try it again.
				return ParseStatement(reader, environment);
			}

			if(environment != null)
			{
				Water.Blocks.Block block = GetBlock(expressions, environment);
				if(block != null)
				{
					Water.Statement statement = new Water.Statement(file, line, column, expressions);

					expressions.Add(
						ParseBlockStatements(
						reader, 
						environment, 
						block.Name, 
						"end_" + block.Name, 
						block.IsRecursive, 
						statement));

					return statement;
				}
			}

			return new Water.Statement(file, line, column, expressions);
		}

		private static Water.List ParseBlockStatements(Water.TextReader reader, Water.Environment environment, string name, string end, bool allowRecursion, Water.Statement start_statement)
		{
			Water.List block_statements = new Water.List();

			while(reader.CanRead)
			{
				Water.Parser.TrimWhitespace(reader);

				Water.Statement statement = ParseStatement(reader, environment);

				if (statement.Expression.Count == 1 && statement.Expression[0].ToString().StartsWith(end))
				{
					return block_statements;
				}

				block_statements.Add(statement);
			}

			environment.Push("_Statement", start_statement);
			throw new System.Exception("\"" + end + "\" expected.");
		}

		private static Water.Blocks.Block GetBlock(Water.List list, Water.Environment environment)
		{
			if(list == null || list.Count == 0)
			{
				return null;
			}

			if(!(list[0] is Water.Identifier))
			{
				return null;
			}

			Water.Identifier identifier = (Water.Identifier)list[0];

			object block = environment.Identify(identifier.Value);

			if(block == null)
			{
				return null;
			}

			if(!(block is Water.Blocks.Block))
			{
				return null;
			}

			return (Water.Blocks.Block)block;
		}

	}
}
